import request from '@/utils/request';
import { ResponseData } from '../typing';

export type LoginType = 'account' | 'telephone';
export type LoginStatus = 'ok' | 'error';

export interface LoginParams {
    type: LoginType;
    username: string;
    password: string;
}

export interface LoginResp {
    accessToken: string;
    expiresIn: number;
    refreshToken: string;
    tokenHeader: string;
    tokenType: string;
    userinfo?: any;
}

export enum Action {
    ADD = 'add',
    DELETE = 'delete',
    UPDATE = 'update',
    QUERY = 'query',
    IMPORT = 'import',
    EXPORT = 'export',
}

export interface Permission {
    /* 权限ID */
    id: string;
    /* 权限归属的角色 */
    roleId?: string | number;
    /* 权限名称 */
    name: string;
    /* 权限显示的名字 */
    label?: string;
    /* 权限拥有的操作 [增,删,改,查] */
    actions?: Action[];
}

export interface Role {
    /* 角色ID */
    id?: string;
    /* 角色名称 */
    name: string;
    code: string;
    /* 角色描述 */
    remark?: string;
    status: number;
    himself: number;
    /* 角色绑定的权限 */
    // permissions?: Permission[];
    postIds?: string[];
    menuIds?: string[];
}

export interface UserInfo {
    createTime: Record<string, unknown>;
    updateTime: Record<string, unknown>;
    creator: number;
    updater: number;
    // 用户ID */
    id: number;
    // 用户名 */
    username: string;
    // 密码 */
    password: string;
    // 头像地址 */
    avatar: string;
    // 姓名 */
    name: string;
    // 员工编号 */
    code: string;
    // 状态:0->禁用,1->启用 */
    status: number;
    // 工作手机号 */
    workPhone: string;
    // 工作手机号 */
    personalPhone: string;
    // 岗位编号 */
    jobCode: string;
    // 岗位名称 */
    jobName: string;
    // 专业条线 */
    professionalLine: string;
    // 负责区域 */
    projectNames: string;
    // 过期时间 */
    expireTime: Record<string, unknown>;
}

export interface CaptchaResp {
    captcha: number;
}

export interface SmsCaptchaRequest {
    mobile: string;
}

// 后端的结构体定义
export type RouteItem = {
    id: number | string;
    parentId: number | string;
    name: string;
    path: string;
    redirect: string;
    component: string;
    meta: {
        title: string | false;
        icon?: string;
        target?: '_blank' | '_self';
        hideInMenu?: boolean;
        hideChildrenInMenu?: boolean;
        hideInBreadcrumb?: boolean;
        authority?: string | string[];
        [key: string]: any;
    };
};

export async function postAccountLogin(params: any) {
    return request.post<LoginParams, ResponseData<LoginResp>>('/oauth2/token', params, {
        headers: {
            'Content-Type': 'multipart/form-data',
            Authorization: 'Basic eWZ3eS1saWZlOjVkcEpZNnVKZGxTdXhmVWFUYUQyS0lpYmJoa3pDR21z',
        },
    });
}

// 响应接口
export interface GetUserRes {
    code: number;
    data: UserInfo;
    msg: string;
}
/**
 * 获得用户信息
 * @param {string} id
 * @returns
 */
export function getCurrentUser(id: number): Promise<GetUserRes> {
    return request.get(`/system/user/get/${id}`);
}

export async function getCurrentUserPermission() {
    return request.get<any, UserInfo>('/oauth/userinfo/get-permission-info');
}

export async function getCurrentUserNav() {
    // return request.get<any, RouteItem[]>('/currentUserNav');
    return request.get<any, any>('/oauth/userinfo/get-permission-info');
}

export async function postLogout(params: any) {
    return request.post<any, any>('/oauth2/revoke', params || {}, {
        headers: {
            'Content-Type': 'multipart/form-data',
            Authorization: 'Basic eWZ3eS1saWZlOjVkcEpZNnVKZGxTdXhmVWFUYUQyS0lpYmJoa3pDR21z',
        },
    });
}

export async function getSmsCaptcha(params: SmsCaptchaRequest) {
    return request.get<SmsCaptchaRequest, CaptchaResp>('/message/captcha/sms', {
        params,
    });
}


// 响应接口
export interface NeedPwdChangeRes {
  code: number;
  data: {
    // 是否需要修改密码:0->否,1->是 */
    needChangePwd: number;
    // 角色 */
    roles: string;
  };
  msg: string;
}

/** 
 * 是否需要修改密码-角色列表
 * @returns
 */
export function needPwdChange(): Promise<NeedPwdChangeRes> {
  return request.get(`/system/pwd/pwdchanged`);
}